package com.eascs.admin.o2o.loanAccount.serviceimpl;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.account.api.IAccountService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loanAccount.dao.O2oLoanapplyAccountDao;
import com.eascs.admin.o2o.loanAccount.entity.O2oLoanapplyAccountEntity;
import com.eascs.admin.o2o.loanAccount.service.IAccountLogService;
import com.eascs.admin.o2o.loanAccount.service.Io2oLoanapplyAccountService;
import com.eascs.admin.o2o.loanAccount.vo.O2oCollectFeesVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.notify.mail.MailNotifier;
import com.eascs.web.sso.UserHolder;

@Service
public class O2oLoanapplyAccountImpl implements Io2oLoanapplyAccountService{
	
	
	@Autowired
	O2oLoanapplyAccountDao o2oLoanapplyAccountDao;
	@Reference
	IAccountService iAccountService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;
	@Reference
	IAccountLogService iAccountLogService;
	
	@Reference
	private MailNotifier mailNotifier;
	
	@Autowired
	O2oLoanapplyAccountservice o2oLoanapplyAccountservice;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	public O2oLoanapplyAccountEntity getEntityById(String id) {
		return o2oLoanapplyAccountDao.getById(O2oLoanapplyAccountEntity.class, id);
	}
	
	@Override
	public O2oLoanapplyAccountEntity getEntityByLoanapplyNo(String id) {
		logger.info("find o2o_loanapply_account table data where loanapply_no:{}", id);
		String sql = "SELECT ol.* from o2o_loanapply_account ol WHERE  ol.loanapply_no = ? ";
		return o2oLoanapplyAccountDao.findEntity(sql, O2oLoanapplyAccountEntity.class, id);
	}
	
	
	@Override
	public int update(O2oLoanapplyAccountEntity arg0) {
		
		return o2oLoanapplyAccountDao.updateById(arg0);
	}


	@Override
	public int add(O2oLoanapplyAccountEntity o2oLoanapplyAccountEntity) {

		return o2oLoanapplyAccountDao.insert(o2oLoanapplyAccountEntity);
	}
	
	@Override
	public void OpenLoanAccount(){
		o2oLoanapplyAccountservice.OpenLoanAccount();
	}
	
	@Override
	public void LoanAccountBZJHK(){
		o2oLoanapplyAccountservice.LoanAccountBZJHK();
	}

	@Override
	public PageResult<Object[]> findLoanAccountByPage(O2oCollectFeesVo vo) {
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<Object[]>(Object[].class,vo.getPageSize() ,
				vo.getPageIndex() );
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT l.loanapply_no, u.user_full_name,l.bill_code,l.wf_type,e.bill_status from o2o_loanapply l ");
		sql.append(" LEFT JOIN o2o_loan_user u on l.loan_id=u.loan_id and u.spouse_id is null and u.sponsor_type is null ");
		sql.append(" LEFT JOIN o2o_loanapply_expand e on e.loanapply_no=l.loanapply_no ");
        sql.append(" where l.islock='N' and l.loanapply_no=u.loanapply_no ");
		sql.append(" and (l.creat_ro="+UserHolder.getUser().getNo());
		sql.append(" or /**DATA_PERMISSION(l.LTD)**/  1=1 ) ");
		
		if (StringUtil.hasText(vo.getBillCode())) {
			sql.append(" and l.bill_code like ?");
			pageQuery.addLikeParam(vo.getBillCode());
		}
		
		if (StringUtil.hasText(vo.getLoanName())) {
			sql.append(" and user_full_name like ?");
			pageQuery.addLikeParam(vo.getLoanName());
		}
		sql.append(" ORDER BY l.create_date desc ");
	
		pageQuery.setQuerySql(sql.toString());
		
		return o2oLoanapplyAccountDao.findDataWithPage(pageQuery);
	}
	
}
